package com.woniuxy.main.configuration;

import com.woniuxy.main.realm.StaffRealm;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

/*
 *
 *  @author zhc
 *  @create 2019-08-05 10:05
 */
@Configuration //将当前类指定为配置类  等价于 shiro的xml
public class ShiroConfiguration {
    //配置realm
    @Bean     //<bean>
    public StaffRealm getReam() {
        StaffRealm realm = new StaffRealm();
        realm.setCredentialsMatcher(hashedCredentialsMatcher());
        System.out.println("创建realm:"+realm);
        return realm;
    }
    //加密
    /*
    <bean class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
				<property name="hashAlgorithmName" value="MD5"></property>
				<property name="hashIterations" value="1024"></property>
			</bean>
     */
    @Bean
    public HashedCredentialsMatcher hashedCredentialsMatcher(){
        System.out.println("MD5加密");
        HashedCredentialsMatcher matcher = new HashedCredentialsMatcher();

        matcher.setHashAlgorithmName("MD5");
        matcher.setHashIterations(1024);

        return matcher;
    }
    //缓存

    //安全管理器
    @Bean
    public DefaultWebSecurityManager getManager(StaffRealm realm) {
        System.out.println("manager:"+realm);
        /*
        <property name="realm" ref="myRealm"></property>
        <!-- 6.3 配置session 管理器-->
        <property name="sessionManager" ref="sessionManager"></property>
        <!-- 7.2 配置缓存管理器-->
        <property name="cacheManager" ref="cacheManager"></property>
        <!-- 8.2 配置cookie 过期时间:单位为秒-->
        <property name="rememberMeManager.cookie.maxAge" value="20"></property>
         */
        DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
        manager.setRealm(realm);





        return manager;
    }
    //过滤器
    @Bean
    public ShiroFilterFactoryBean getFactoryBean(DefaultWebSecurityManager manager) {
        System.out.println("创建shiro过滤器");
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        bean.setSecurityManager(manager);
        //登录页面
        bean.setLoginUrl("/index.html");
        //没有权限
        bean.setUnauthorizedUrl("/html/error.html");
        //过滤器链
        Map<String, String> map = new LinkedHashMap<>();//需要保证有序
        map.put("/index.html", "anon");
        map.put("/empl/login", "anon");

        //静态放行
        map.put("/css/**", "anon");
        map.put("/js/**", "anon");
        map.put("/html/**", "anon");
        map.put("/img/**", "anon");
        map.put("/images/**", "anon");
        map.put("/ayui/**", "anon");


        //rememberme
        map.put("/frame.html","user");

        //放行druid的操作
        map.put("/druid/**", "anon");
        map.put("/**", "anon");


        bean.setFilterChainDefinitionMap(map);

        return bean;
    }

}
